// pages/daixiaoDingdan/shenheAndfahuo/shenheAndfahuo.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flagIndex: 0, //0待付款*3待取货*4待评价*5申请退款*6已退款*7已完成*8店铺结算*
    //0待付款*1待取货*2待评价*3申请退款*4已退款*5已完成*6店铺结算*
    kuaidiExist: false, //快递是否存在
    dingdanid:'',
    dingdaninfo:'',
    timer: '', //定时器名称
    countDownList: [], //显示时间列表
    actEndTimeList: [], //时间列表
    hiddenmodalput: true,//拒绝退款弹窗
    jujueinfo: '',//拒绝退款原因
  },
  timeFormat: function timeFormat(param) {
    //小于10的格式化函数
    return param < 10 ? "0" + param : param;
  },
  //获取时间
  countDown: function countDown() {
    console.log('倒计时')
    var that = this;
    //倒计时函数
    // 获取当前时间，同时得到活动结束时间数组
    var newTime = new Date().getTime();
    var endTimeList = that.data.actEndTimeList;
    var countDownList = [];
    // 对结束时间进行处理渲染到页面
    endTimeList.forEach(function (o) {
      var endTime = new Date(o).getTime();
      var obj = null;
      var time = 0;
      // 如果活动未结束，对时间进行处理
      if (endTime - newTime > 0) {
        var _time = (endTime - newTime) / 1000;
        // 获取天、时、分、秒
        var day = parseInt(_time / (60 * 60 * 24));
        var hou = parseInt(_time % (60 * 60 * 24) / 3600);
        var min = parseInt(_time % (60 * 60 * 24) % 3600 / 60);
        var sec = parseInt(_time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          day: that.timeFormat(day),
          hou: that.timeFormat(hou),
          min: that.timeFormat(min),
          sec: that.timeFormat(sec)
        };
        // obj = obj.day + "天" + obj.hou + ":" + obj.min + ":" + obj.sec;
      } else {
        that.clear()
        //活动已结束，全部设置为'00'
        obj = {
          day: "00",
          hou: "00",
          min: "00",
          sec: "00"
        };
        /// -------
        if (that.data.ifstart == 2) {
          that.getgoodsinfo()
        } else {
          wx.showToast({
            title: '活动已结束',
            icon: 'none'
          })
          that.setData({
            ifstart: 3
          })
          return
        }

        // obj = obj.day + "天" + obj.hou + ":" + obj.min + ":" + obj.sec;
      }

      countDownList.push(obj);
      // console.log(that.data.countDownList)
    });
    that.setData({
      countDownList: countDownList
    })
    if (that.data.ifstart == 3) {
      that.clear()
    } else {
      var timer = setTimeout(that.countDown, 1000);
      setTimeout(function () {
        that.setData({
          timer: timer
        })
      })
    }
    // console.log(that.data.countDownList)

  },
  // 关闭定时器
  clear() {
    let that = this
    clearTimeout(that.data.timer)
    this.setData({
      timer: ''
    })
  },
  // 是否同意退款
  refundsagreed() {
    let that = this
    wx.showModal({
      title: '是否同意退款',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.myurl,
            data: {
              cmd: "updRefund",
              shopId: wx.getStorageSync('shopId'),
              userId: wx.getStorageSync('userId'),
              orderId: that.data.dingdanid,
              state: 1,
              remarks: '121'
            },
            header: {
              "content-type": "application/x-www-form-urlencoded",
              "token": wx.getStorageSync('token')
            },
            success(res) {
              if (res.data.zjstate == 1) {
                setTimeout(function () {
                  wx.hideLoading()
                  wx.stopPullDownRefresh() //停止下拉刷新
                }, 0)
                setTimeout(function () {
                  wx.showModal({
                    title: '您的账号已在其他设备登录，请重新登录！',
                    showCancel: false, //是否显示取消按钮false去掉取消按钮
                    success: function (res) {
                      if (res.confirm) {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      } else {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      }
                    }
                  })
                }, 0)
                return
              }
              console.log(res)
              if (res.data.result == 1) {
                setTimeout(function () {
                  wx.hideLoading()
                }, 0)
                setTimeout(function () {
                  wx.showToast({
                    title: res.data.resultNote,
                    icon: 'none',
                    duration: 1500
                  })
                }, 0)
              } else {
                setTimeout(function () {
                  wx.hideLoading()
                }, 0)
                setTimeout(function () {
                  wx.showToast({
                    title: res.data.resultNote,
                    icon: 'none',
                    duration: 1500
                  })
                }, 0)
                setTimeout(function () {
                  wx.showLoading({
                    title: '加载中',
                  })
                  that.getdingdaninfo()
                }, 1500)

              }
            }
          });
        } else {
          console.log('点击取消回调')
        }
      }
    })
  },
  // 是否拒绝退款
  refusedrefund() {
    let that = this
    that.setData({
      hiddenmodalput: false
    })
    // wx.showModal({
    //   title: '是否拒绝退款',
    //   success: function (res) {
    //     if (res.confirm) {
    //       wx.request({
    //         url: app.globalData.myurl,
    //         data: {
    //           cmd: "updRefund",
    //           shopId: wx.getStorageSync('shopId'),
    //           userId: wx.getStorageSync('userId'),
    //           orderId: that.data.dingdanid,
    //           state: 2,
    //           remarks:'121'
    //         },
    //         header: {
    //           "content-type": "application/x-www-form-urlencoded",
    //           "token": wx.getStorageSync('token')
    //         },
    //         success(res) {
    //           if (res.data.zjstate == 1) {
    //             setTimeout(function () {
    //               wx.hideLoading()
    //               wx.stopPullDownRefresh() //停止下拉刷新
    //             }, 0)
    //             setTimeout(function () {
    //               wx.showModal({
    //                 title: '您的账号已在其他设备登录，请重新登录！',
    //                 showCancel: false, //是否显示取消按钮false去掉取消按钮
    //                 success: function (res) {
    //                   if (res.confirm) {
    //                     wx.clearStorageSync()
    //                     wx.reLaunch({
    //                       url: '/pages/login/login',
    //                     })
    //                   } else {
    //                     wx.clearStorageSync()
    //                     wx.reLaunch({
    //                       url: '/pages/login/login',
    //                     })
    //                   }
    //                 }
    //               })
    //             }, 0)
    //             return
    //           }
    //           console.log(res)
    //           if (res.data.result == 1) {
    //             setTimeout(function () {
    //               wx.hideLoading()
    //             }, 0)
    //             setTimeout(function () {
    //               wx.showToast({
    //                 title: res.data.resultNote,
    //                 icon: 'none',
    //                 duration: 1500
    //               })
    //             }, 0)
    //           } else {
    //             setTimeout(function () {
    //               wx.hideLoading()
    //             }, 0)
    //             setTimeout(function () {
    //               wx.showToast({
    //                 title: res.data.resultNote,
    //                 icon: 'none',
    //                 duration: 1500
    //               })
    //             }, 0)
    //             setTimeout(function () {
    //               wx.showLoading({
    //                 title: '加载中',
    //               })
    //               that.getdingdaninfo()
    //             }, 1500)

    //           }
    //         }
    //       });
    //     } else {
    //       console.log('点击取消回调')
    //     }
    //   }
    // })
  },
  getjujueinfo(e) {
    console.log(e)
    this.setData({
      jujueinfo: e.detail.value
    })
  },
  // 拒绝退款确定
  confirmjujue() {
    let that = this
    console.log('拒绝退款确定')
    if (!that.data.jujueinfo) {
      wx.showToast({
        title: '请填写拒绝退款原因',
        icon: 'none'
      })
      return
    }
    setTimeout(function () {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "updRefund",
        shopId: wx.getStorageSync('shopId'),
        userId: wx.getStorageSync('userId'),
        orderId: that.data.dingdanid,
        state: 2,
        remarks: that.data.jujueinfo
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        that.setData({
          hiddenmodalput: true
        })
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function () {
            wx.hideLoading()
          }, 0)
          setTimeout(function () {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          setTimeout(function () {
            wx.hideLoading()
          }, 0)
          setTimeout(function () {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
          setTimeout(function () {
            wx.showLoading({
              title: '加载中',
            })
            that.getdingdaninfo()
          }, 1500)

        }
      }
    });
  },
  // 拒绝退款取消
  canceljujue() {
    let that = this
    that.setData({
      hiddenmodalput: true
    })
    console.log('拒绝退款取消')
  },
  // 去商品详情
  togoodsxq(e) {
    // let that = this
    // let goodsid = e.currentTarget.dataset.goodsid
    // wx.navigateTo({
    //   url: '/pages/goods/page/goodsXiangqing/daixiaoxianshiGoods/daixiaoxianshiGoods?goodsid=' + goodsid,
    // })
  },
  getdingdaninfo() {
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "getSellOffOrderDetail",
        shopId: wx.getStorageSync('shopId'),
        userId: wx.getStorageSync('userId'),
        id: that.data.dingdanid
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function () {
            wx.hideLoading()
          }, 0)
          setTimeout(function () {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          var endTimeList = []
          if (res.data.orderBean.state == 0) {
            var str = res.data.orderBean.endtime.replace(/-/g, "/")
            endTimeList.push(str)
            that.setData({
              actEndTimeList: endTimeList,
            })
            that.countDown()
          }
          that.setData({
            dingdaninfo: res.data.orderBean,
            guigeinfo: res.data.orderBean.goodsList[0].skuBeans[0],
          })
          setTimeout(function () {
            wx.hideLoading()
          }, 0)
        }
      }
    });
  },
  tochaoshi() {
    // let that = this
    // wx.navigateTo({
    //   url: '/pages/setting/page/dianpu/daixiaodianpu/daixiaodianpu?dingdanid=' + that.data.dingdanid + '&resshopid=' + that.data.dingdaninfo.shopid + '&type=2'
    // })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    let that = this
    that.setData({
      dingdanid: options.dingdanId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    setTimeout(function () {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    let that = this
    that.getdingdaninfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    let that = this
    that.clear()
    // clearTimeout(that.data.timer)
    // this.setData({
    //   timer:''
    // })

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this
    that.clear()
    // clearTimeout(that.data.timer)
    // this.setData({
    //   timer: ''
    // })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})